package com.tamtamy.jtta.data.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.tamtamy.jtta.data.NoticeList;
import com.tamtamy.jtta.exception.NoResultException;
import com.tamtamy.jtta.exception.ParsingException;

public class XMLNoticeList extends NoticeList{
	
	private static final long serialVersionUID = -2288504097535308796L;
	
	private static final String KCLNOTICE = "it.reply.tt.rest.entity.KcLNotice";
	
	public XMLNoticeList(Document xmlDocument) throws NoResultException, ParsingException {
		super();
		NodeList noticeNodeList = xmlDocument.getElementsByTagName(KCLNOTICE);
		if (noticeNodeList == null || noticeNodeList.getLength()==0){
			throw new NoResultException("No " + KCLNOTICE + " element found");
		}
		for (int i = 0; i < noticeNodeList.getLength(); i++) {
			Node noticeNode = noticeNodeList.item(i);
			try {
				Document noticeDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
				noticeDocument.appendChild(noticeDocument.importNode(noticeNode, true));
				this.add(new XMLNotice(noticeDocument)); 
			} catch (ParserConfigurationException parserConfigurationException) {
				throw new ParsingException("Error parsing " + KCLNOTICE + " element : " + parserConfigurationException.getMessage());
			}   
		}				
	}
}
